#!/bin/bash

BINARY=bin/ompi-samebatch-multiproduct-binpacking
MIN_PERCENT_FOR_DISPATCHING=50
MAX_PERCENT_FOR_DISPATCHING=60
PERIOD_MPI_TEST_MASTER=10
PERIOD_MPI_TEST_SLAVE=10
ADRESS_MAIL="olivier.grunder@utbm.fr"

if [ ! -f ${BINARY} ]; then
    echo ""
    echo "ERROR ${BINARY} does not exists here: "
    echo "  current dir=`pwd`"
    echo ""
    exit 1
fi

if (( $# == 0 )) 
then
    echo ""
    echo "Usage:"
    echo ""
    echo "  script/qsubinstance data_file [queue=15d] [nbrnodes=128]"
    echo ""
    echo "Creates a sge script with :"
    echo "  queue                       : the queue '2h' or '15d' "
    echo "  nbrnodes                    : number or nodes (max 16 for '2h' and 128 for '15d')"
    echo "  data_file                   : the problem to load"
    echo ""
    echo "Exemple :"
    echo "  script/qsubinstance data/instance040c1.txt 2h 16"
    exit 1
fi

# INSTANCE OF THE PROBLEM
DATA_FULLPATH=$1

if [ ! -f ${DATA_FULLPATH} ]; then
    echo ""
    echo "ERROR data file '${DATA_FULLPATH}' does not exists"
    echo ""
    exit 1
fi

# QUEUE OF THE CLUSTER : "2h" for 2hours or "15d" for 15 days
QUEUE="15d"

if (( $# > 1 ))
then
    QUEUE=$2
    if [ "${QUEUE}" = "2H" ]; then
	QUEUE="2h"
    fi
    if [ "${QUEUE}" == "15D" ]; then
	QUEUE="15d"
    fi
fi
echo "Queue=${QUEUE}"

# NBR OF NODES TO RUN THE MPI APPLICATION : MAX 16 for "2h" and 128 for "15d"
NBR_NODES=128
if [ "${QUEUE}" == "2h" ]
then
    NBR_NODES=16
fi

if (( $# > 2 ))
then
    NBR_NODES=$3
fi
echo "NbrNodes=${NBR_NODES}"

# START TO WRITE THE SGE SCRIPT
datafullfilename=$(basename ${DATA_FULLPATH})
datafilename=${datafullfilename%.*}
dataextension=${datafullfilename##*.}
    
echo "datafullfilename=${datafullfilename}"
echo "datafilename=${datafilename}"
echo "dataextension=${dataextension}"
    
SGE_FULLPATH="sge/${datafilename}_${QUEUE}_${NBR_NODES}_ompi.sge"

# Sauvegarde du script sge existant
if [ -f ${SGE_FULLPATH} ]; then
    mv ${SGE_FULLPATH} ${SGE_FULLPATH}-`date +%y%m%d-%T`.sav
fi

BINARY_PARAM="${BINARY} -bbp ${DATA_FULLPATH} -periodMPITestMaster ${PERIOD_MPI_TEST_MASTER} -periodMPITestSlave ${PERIOD_MPI_TEST_SLAVE} -minPercentForDispatching ${MIN_PERCENT_FOR_DISPATCHING} -maxPercentForDispatching ${MAX_PERCENT_FOR_DISPATCHING}"

echo "#!/bin/bash -l                                   " >  ${SGE_FULLPATH} 
echo "                                                 " >> ${SGE_FULLPATH}
echo "## export shell to execution hosts               " >> ${SGE_FULLPATH}
echo "#$ -V                                            " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "# Nom du job                                     " >> ${SGE_FULLPATH}
echo "#$ -N ${datafilename}_${QUEUE}_${NBR_NODES}_ompi " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "### we request normal15d queue                   " >> ${SGE_FULLPATH}
echo "#$ -q normal${QUEUE}                             " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "## request ${NBR_NODES} nodes !!                 " >> ${SGE_FULLPATH}
echo "#$ -pe impi_tight ${NBR_NODES}                   " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "## request 4G memory for the job                 " >> ${SGE_FULLPATH}
echo "#$ -l mem_free=4G                                " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "# Current working dir                            " >> ${SGE_FULLPATH}
echo "#$ -cwd                                          " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "# Notification en debut, fin et interruption     " >> ${SGE_FULLPATH}
echo "#$ -M ${ADRESS_MAIL}                             " >> ${SGE_FULLPATH}
echo "#$ -m bea                                        " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "# Nom du fichier ou stocker la sortie            " >> ${SGE_FULLPATH}
echo "#$ -o log/\$JOB_NAME.o\$JOB_ID.log               " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "# Nom du fichier ou stocker les erreurs          " >> ${SGE_FULLPATH}     
echo "#$ -e log/\$JOB_NAME.e\$JOB_ID.log               " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "#### on charge le module open mpi                " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "module rm ompi                                   " >> ${SGE_FULLPATH}
echo "module load ompi                                 " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "## lancement de l'application                    " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "function runmailwrapper()                        " >> ${SGE_FULLPATH}
echo "{                                                " >> ${SGE_FULLPATH}
echo "  time mpirun -machinefile \$TMPDIR/machines -np \$NSLOTS ${BINARY_PARAM}" >> ${SGE_FULLPATH}
echo "  mail ${ADRESS_MAIL} < log/\$JOB_NAME.o\$JOB_ID.log" >> ${SGE_FULLPATH}
echo "}                                                " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}
echo "runmailwrapper                                   " >> ${SGE_FULLPATH}
echo "                                                 " >> ${SGE_FULLPATH}

# FINALLY, SUBMIT THE SGE SCRIPT TO THE QUEUE
qsub ${SGE_FULLPATH}

